package com.cade.agriculturemall.view.shoppingCar.selecpaystatus;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SaveCallback;
import com.cade.agriculturemall.R;
import com.cade.agriculturemall.base.BaseActivity;
import com.cade.agriculturemall.base.IBasePresenter;
import com.cade.agriculturemall.model.GoodsDetailEntity;
import com.cade.agriculturemall.model.OrderEntity;
import com.cade.agriculturemall.model.UserInfoEntity;
import com.cade.agriculturemall.view.personalCenter.ordersManage.OrderItems.OrderActivity;

import java.util.ArrayList;
import java.util.List;

public class SelectPayStatusActivity extends BaseActivity implements View.OnClickListener {


    ArrayList<String> orderList = new ArrayList<>();
    private String orderId = "";
    private OrderEntity orderEntity;
    private String dealId;
    private ArrayList list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_pay_status);
        Button btnSuccess, btnFail, btnCancel;
        btnSuccess = (Button) findViewById(R.id.btn_pay_status_success);
        btnCancel = (Button) findViewById(R.id.btn_pay_status_cancel);
        btnFail = (Button) findViewById(R.id.btn_pay_status_fail);
        btnSuccess.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        btnFail.setOnClickListener(this);

        orderList = getIntent().getStringArrayListExtra("orderList");

        dealId = (getIntent().getStringExtra("dealNo"));//String  8215199809173929aa5           0615199812860519aa5
        ticketId = getIntent().getStringExtra("ticketId");
        orderId = getIntent().getStringExtra("orderId");

//        由商品详情直接购买
        Bundle bundle = getIntent().getExtras();
        orderId = bundle.getString("orderId");
        list = bundle.getParcelableArrayList("goodsDetailEntity");
        List<OrderEntity> orderEntities = bundle.getParcelableArrayList("orderEntity");
        if (orderEntities != null)
            orderEntity = orderEntities.get(0);
//        dealId = bundle.getString("");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_pay_status_success:
                if (!TextUtils.isEmpty(ticketId)) {
                    useTicket(ticketId);
                }
                setOrderStatus(getResources().getString(R.string.order_status_pay_wait_deliver));

                break;
            case R.id.btn_pay_status_cancel:
                setOrderStatus(getResources().getString(R.string.order_status_no_pay));

                break;
            case R.id.btn_pay_status_fail:
                setOrderStatus(getResources().getString(R.string.order_status_no_pay));
                break;
        }
    }

    String ticketId = "";

    public void goNext() {
        String dealNo = getIntent().getStringExtra("dealNo");
        Intent intent = new Intent(this, OrderActivity.class);
        if (!TextUtils.isEmpty(dealNo)) {
            finish();
            intent.putExtra("dealNo", dealNo);
        }
        if (!TextUtils.isEmpty(orderId)) {
            intent.putExtra("orderNo", orderId);
        }
        startActivity(intent);
        finish();
    }

    public void setOrderStatus(String orderStatus) {
        showProgress();
        if (orderList.size() != 0) {
            for (Object objectId : orderList) {
                AVObject avObject = AVObject.createWithoutData("order", (String) objectId);
                avObject.put("order_status", orderStatus);
                avObject.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(AVException e) {
                        if (e == null) {
                            goNext();
                        } else {
                            e.printStackTrace();
                        }
                        dismissProgress();
                    }
                });
            }
        } else {
            Toast.makeText(this, "isEmpty", Toast.LENGTH_SHORT).show();
        }
    }

    public void useTicket(String ticketId) {
        AVObject avObject = AVObject.createWithoutData("ticket_discount", ticketId);
        avObject.put("had_use", "2");
        avObject.saveInBackground(new SaveCallback() {
            @Override
            public void done(AVException e) {
                if (e != null) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void setupView() {

    }

    @Override
    public void setupEvent() {

    }

    @Override
    public void setupData() {

    }

    @Override
    public IBasePresenter setupPresenter() {
        return null;
    }


    final UserInfoEntity user = AVUser.getCurrentUser(UserInfoEntity.class);


}

